angular.module('gather-manage', [])

.controller 'GatherManage', ($scope, $state, $http, $ionicHistory, $stateParams,userData,showToast) ->
    _.extend $scope,
        gather:
            alipay:undefined
            wxpay:undefined
            bankpay:undefined
            offlinepay:undefined
            overdraftpay:undefined
            accountpay:undefined
        goBack:->
            $ionicHistory.goBack()
        goAccount:->
            $state.go 'tab.bank-account-list'
        updateGather:->
            gh=$scope.gather
            if gh.alipay || gh.wxpay || gh.bankpay || gh.offlinepay || gh.overdraftpay || gh.accountpay
                requestData={
                    action:'savepayment'
                    userid:userData.getToken()
                    alipay:if $scope.gather.alipay then 1 else 0
                    wxpay:if $scope.gather.wxpay then 1 else 0
                    bankpay:if $scope.gather.bankpay then 1 else 0
                    offlinepay:if $scope.gather.offlinepay then 1 else 0
                    overdraftpay:if $scope.gather.overdraftpay then 1 else 0
                    accoutpay:if $scope.gather.accountpay then 1 else 0
                }
                console.log requestData
                $http.post('/utf-8data/',requestData)
                .success (resp) ->
                    if resp?.status=='0'
                        showToast '修改成功'
                    else
                        showToast '修改失败'
                .error (err) ->
                    console.log err
                    showToast '网络异常,修改失败'
            else
                showToast '至少选择一种付款方式'
    loadGatherInfo=()->
        $http.get('/seller/',params:{action:'getpayment',id:$scope.itemId,userid:userData.getToken()})
        .success (resp) ->
            if resp?.status == '0'
                $scope.gather.alipay=if resp.alipay==1 then true else false
                $scope.gather.wxpay=if resp.wxpay==1 then true else false
                $scope.gather.bankpay=if resp.bankpay==1 then true else false
                $scope.gather.offlinepay=if resp.offlinepay==1 then true else false
                $scope.gather.overdraftpay=if resp.overdraftpay==1 then true else false
                $scope.gather.accountpay=if resp.accoutpay==1 then true else false
            else
                angular.noop()
        .error ->
            showToast '服务器处理失败'
    $scope.$on '$ionicView.enter',->
        loadGatherInfo()